package com.xr.oa.fileShare;

import java.util.List;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * fileDeptRoleSetViewModel
 */
public class fileDeptRoleSetViewModel extends DefaultViewModel {
  public void deptRoleSetSave(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
	Dataset comDs= getDataset("datasetCom");
    Dataset deptDs= getDataset("datasetDept");
    Dataset empDs= getDataset("datasetEmp");
    Dataset roleDs= getDataset("datasetPosition");
    String filesId=deptDs.parameters().getString("files_id");
    DBStatement dbs = new DBStatement();
    int fileType=parameters.getInt("TYPE");
    comDs.moveFirst();
    while (!comDs.isLast()) {
      if(comDs.getBoolean("select")){ 
    	 // System.out.println("======"+deptDs.getString("SUPER_DEPT_ID"));
    	  dbs.setSql("select * from oa_files_dept_role where FILES_ID="+filesId
    	  		+" and DEPT_ROLE_ID="+comDs.getString("COMPANY_ID")+" and TYPE=4");
    	  List list=dbs.queryForList();
    	  
    	  if(list.size()==0){
    	  dbs.setSql("insert into oa_files_dept_role" +
    	  		"(FILES_DEPT_ROLE_ID,FILES_ID,DEPT_ROLE_ID,TYPE)" +
    	  		"values(general.nextval,"+filesId+","+comDs.getString("COMPANY_ID")+",4)");
    	  dbs.execute();
    	  }
      }else{
    	  dbs.setSql("delete from oa_files_dept_role " +
    	  		"where FILES_ID="+filesId
    	  		+" and DEPT_ROLE_ID="+comDs.getString("COMPANY_ID")+" and TYPE=4");
      	  dbs.execute();
      }
      comDs.moveNext();
    }
    
    deptDs.moveFirst();
    while (!deptDs.isLast()) {
      if(deptDs.getBoolean("select")){ 
    	 // System.out.println("======"+deptDs.getString("SUPER_DEPT_ID"));
    	  dbs.setSql("select * from oa_files_dept_role where FILES_ID="+filesId
    	  		+" and DEPT_ROLE_ID="+deptDs.getString("DEPT_ID")+" and TYPE=1");
    	  List list=dbs.queryForList();
    	  
    	  if(list.size()==0){
    	  dbs.setSql("insert into oa_files_dept_role" +
    	  		"(FILES_DEPT_ROLE_ID,FILES_ID,DEPT_ROLE_ID,TYPE)" +
    	  		"values(general.nextval,"+filesId+","+deptDs.getString("DEPT_ID")+",1)");
    	  dbs.execute();
    	  }
      }else{
    	  dbs.setSql("delete from oa_files_dept_role " +
    	  		"where FILES_ID="+filesId
    	  		+" and DEPT_ROLE_ID="+deptDs.getString("DEPT_ID")+" and TYPE=1");
      	  dbs.execute();
      }
      deptDs.moveNext();
    }
 
			empDs.moveFirst();
			while (!empDs.isLast()) {
				if (empDs.getBoolean("select")) {
					dbs.setSql("select * from oa_files_dept_role where FILES_ID="
									+ filesId
									+ " and DEPT_ROLE_ID="
									+ empDs.getString("EMPLOYEE_ID")
									+ " and TYPE=3");
					List list = dbs.queryForList();

					if (list.size() == 0) {
						dbs.setSql("insert into oa_files_dept_role"
										+ "(FILES_DEPT_ROLE_ID,FILES_ID,DEPT_ROLE_ID,TYPE)"
										+ "values(general.nextval," + filesId
										+ "," + empDs.getString("EMPLOYEE_ID")
										+ ",3)");
						dbs.execute();
					}
				} else {
					dbs.setSql("delete from oa_files_dept_role "
							+ "where FILES_ID=" + filesId
							+ " and DEPT_ROLE_ID="
							+ empDs.getString("EMPLOYEE_ID") + " and TYPE=3");
					dbs.execute();
				}
				empDs.moveNext();
			}
		  
		
    roleDs.moveFirst();
    while (!roleDs.isLast()) {
        //System.out.println(deptDs.getString("PROVIDER_ID")+"=="+deptDs.getString("PROVIDER_NAME")+"==="+deptDs.getBoolean("select"));
        if(roleDs.getBoolean("ISCHECKED")){
      	  dbs.setSql("select * from oa_files_dept_role where FILES_ID="+filesId
      	  		+" and DEPT_ROLE_ID="+roleDs.getString("POSITION_ID")+" and TYPE=2");
      	  List list=dbs.queryForList();
      	  
      	  if(list.size()==0){
      	  dbs.setSql("insert into oa_files_dept_role" +
      	  		"(FILES_DEPT_ROLE_ID,FILES_ID,DEPT_ROLE_ID,TYPE)" +
      	  		"values(general.nextval,"+filesId+","+roleDs.getString("POSITION_ID")+",2)");
      	  dbs.execute();
      	  }
        }else{
      	  dbs.setSql("delete from oa_files_dept_role " +
      	  		"where FILES_ID="+filesId
      	  		+" and DEPT_ROLE_ID="+roleDs.getString("POSITION_ID")+" and TYPE=2");
        	  dbs.execute();
        }
        roleDs.moveNext();        
      }
   
/*    if (fileType == 2) { // if file

		empDs.moveFirst();
		  String supperFilesId=parameters.getString("SUPPER_FILE_ID");
		while (!empDs.isLast()) {
			if (empDs.getBoolean("select")) {
				
				dbs.setSql("select * from oa_files_dept_role where FILES_ID="
								+ supperFilesId
								+ " and DEPT_ROLE_ID="
								+ empDs.getString("EMPLOYEE_ID")
								+ " and TYPE=3");
				List list = dbs.queryForList();

				if (list.size() == 0) {
					dbs.setSql("insert into oa_files_dept_role"
									+ "(FILES_DEPT_ROLE_ID,FILES_ID,DEPT_ROLE_ID,TYPE)"
									+ "values(general.nextval," + supperFilesId
									+ "," + empDs.getString("EMPLOYEE_ID")
									+ ",3)");
					dbs.execute();
				}
				
			} else {
				
				dbs.setSql("delete from oa_files_dept_role "
						+ "where FILES_ID=" + supperFilesId
						+ " and TYPE=3 and not exists(select 'X' from rt_pvd_dept_employee where employee_id=DEPT_ROLE_ID ) and DEPT_ROLE_ID="
						+ empDs.getString("EMPLOYEE_ID") + " ");
				dbs.execute();
			}
			empDs.moveNext();
		}

	}
    */
  dbs.close();
  }


}
